package com.oco.configuration.mybatis;

import java.util.Properties;

import javax.sql.DataSource;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;


/**
 * MyBatis基础配置.
 * 
 * @author ZhiHong Zhang
 * @email it_javasun@yeah.net
 * @sine 上午11:02:15
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

  /**
   * 数据源.
   */
	@Autowired
  private DataSource dataSource;

	/**
	 * 创建sessionFactoryzz。
	 * @author zhangzhihong
	 * @email it_javasun@yeah.net
	 * @since 2016年8月18日上午9:55:28
	 * @return SqlSessionFactory
	 */
  @Bean(name = "sqlSessionFactory")
  @ConditionalOnMissingBean // 判断是否执行初始化代码，即如果用户已经创建了bean，则相关的初始化代码不再执行。
  public final SqlSessionFactory sqlSessionFactoryBean() {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(dataSource);
      bean.setTypeAliasesPackage("com.oco.mabatis.domain");

      //分页插件
      PageHelper pageHelper = new PageHelper();
      Properties properties = new Properties();
      properties.setProperty("reasonable", "true");
      properties.setProperty("supportMethodsArguments", "true");
      properties.setProperty("returnPageInfo", "check");
      properties.setProperty("params", "count=countSql");
      pageHelper.setProperties(properties);

      //添加插件
      bean.setPlugins(new Interceptor[]{pageHelper});

      //添加XML目录
      ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
      try {
      	Resource[] resource = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+"com/oco/mybatis/mapper/**/*Mapper.xml");
          bean.setMapperLocations(resource);
          return bean.getObject();
      } catch (Exception e) {
          e.printStackTrace();
          throw new RuntimeException(e);
      }
  }

  @Bean
  public final SqlSessionTemplate sqlSessionTemplate(final SqlSessionFactory sqlSessionFactory) {
      return new SqlSessionTemplate(sqlSessionFactory);
  }

  @Bean
  @Override
  public final PlatformTransactionManager annotationDrivenTransactionManager() {
      return new DataSourceTransactionManager(dataSource);
  }
}
